import 'package:flutter/material.dart';

///Table
///1.姓名的升序降序
///2.age的升序降序
class TableDemo2 extends StatefulWidget {
  const TableDemo2({Key? key}) : super(key: key);

  @override
  _TableDemo2State createState() => _TableDemo2State();
}

class _TableDemo2State extends State<TableDemo2> {
  List<Map> _list = [];

  ///排序
  int _sortColumnIndex = 0;
  //升序降序
  bool _sortAscending = true;

  @override
  void initState() {
    for (int i = 1; i < 10; i++) {
      _list.add({
        "name": "h" * i,
        "gender": i % 2 == 0 ? "男" : "女",
        "select": false,
        "age": i,
      });
    }
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("tableDemo2"), centerTitle: true),
      body: Padding(
        padding: EdgeInsets.all(10),
        child: DataTable(
          sortColumnIndex: _sortColumnIndex,
          sortAscending: _sortAscending,
          columns: [
            DataColumn(
                label: Text("姓名"),
                onSort: (int columnIndex, bool ascending) {
                  setState(() {
                    _sortColumnIndex = columnIndex;
                    _sortAscending = ascending;

                    _list.sort((a, b) {
                      if (!ascending) {
                        var c = a;
                        a = b;
                        b = c;
                      }
                      return a["name"]
                          .toString()
                          .length
                          .compareTo(b["name"].toString().length);
                    });
                  });
                }),
            DataColumn(label: Text("年龄"),
                onSort: (int columnIndex, bool ascending) {
                  setState(() {
                    _sortColumnIndex = columnIndex;
                    _sortAscending = ascending;

                    _list.sort((a, b) {
                      if (!ascending) {
                        var c = a;
                        a = b;
                        b = c;
                      }
                      return a["age"]
                          .toString()
                          .compareTo(b["age"].toString());
                    });
                  });
                }),
            DataColumn(label: Text("性别")),
          ],
          rows: _list
              .map((e) => DataRow(
                      selected: e["select"],
                      onSelectChanged: (v) {
                        if (e["select"] != v) {
                          setState(() {
                            e["select"] = v;
                          });
                        }
                      },
                      cells: [
                        DataCell(Text(e["name"])),
                        DataCell(Text(e["age"].toString())),
                        DataCell(Text(e["gender"])),
                      ]))
              .toList(),
        ),
      ),
    );
  }
}
